

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.rmi.PortableRemoteObject;

import sessionbeans.FeedbackBeanRemote;
import entities.RegisteredUser;

/**
 * Servlet implementation class FeedbackServlet
 */
public class FeedbackServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FeedbackServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userId = (String) request.getSession().getAttribute("userId");
		String user = ((RegisteredUser) request.getSession().getAttribute("visitinguser")).getUserId();
		String ability = (String) request.getSession().getAttribute("ability");
		String rating = (String) request.getParameter("rating");
		InitialContext context;
		boolean outcome = false;
		FeedbackBeanRemote feedbackBean;
		System.out.println(user + ' ' + ability + ' ' + userId + ' ' + rating);
		if ((user != null) && (ability != null) && (userId != null) && (rating != null)) {
			try {
				context = new InitialContext();
				Object ref = context.lookup("FeedbackBeanRemote");
				feedbackBean = (FeedbackBeanRemote) PortableRemoteObject.narrow(ref,FeedbackBeanRemote.class);
				int ratingValue = Integer.parseInt(rating);
				outcome = feedbackBean.sendFeedback(userId,user,ability,ratingValue);
			} catch (NamingException e) {
				System.out.println("Naming Exception");
				e.printStackTrace();
			}
			 catch (NumberFormatException e){}
		}
		if(!outcome){
			response.sendRedirect("SetFeedbackPage.jsp?ability=" + ability);
			return;
		}
		response.sendRedirect("ProfileServlet?user=" + user);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
